package cn.zhihong.service.impl;

import cn.zhihong.common.CustomException;
import cn.zhihong.common.R;
import cn.zhihong.dto.DishDto;
import cn.zhihong.pojo.DishFlavor;
import cn.zhihong.service.DishFlavorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhihong.pojo.Dish;
import cn.zhihong.service.DishService;
import cn.zhihong.mapper.DishMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86183
* @description 针对表【dish(菜品管理)】的数据库操作Service实现
* @createDate 2022-04-23 09:49:43
*/
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish>
    implements DishService{
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * @author 庄志宏
     * @date: 2022/4/24 13:07
     * 保存菜品信息
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        //获取dish_id
        Long dishId = dishDto.getId();
        //为每个口味flavor赋值dish_id
//        flavors = flavors.stream().map((item) -> {
//            item.setDishId(dishId);
//            return item;
//        }).collect(Collectors.toList());
        flavors.forEach(flavor->{
            flavor.setDishId(dishId);
        });
        log.info("flavors为：{}",flavors);
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * @author 庄志宏
     * @date: 2022/4/24 12:17
     * 根据id查询菜品信息和相关的口味，这里的id表示的是dish的id，即dish_id
     */
    @Transactional
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据id查询菜品的基本信息，从dish表查询
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
        //查询当前菜品对应的口味信息，从dish_flavors表查询
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * @author 庄志宏
     * @date: 2022/4/24 12:45
     * 修改菜品信息
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        log.info("dishDoto为{}",dishDto);
        //修改菜品表dish的信息
        this.updateById(dishDto);
        //清除当前菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        boolean bool = dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        log.info("是否删除成功{}",bool);
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        List<DishFlavor> flavors = dishDto.getFlavors();
        Long dishId = dishDto.getId();
        flavors.forEach(flavor->{
            flavor.setDishId(dishId);
        });
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * @author 庄志宏
     * @date: 2022/4/24 22:44
     * 根据id删除菜品和菜品口味
     */

    @Transactional
    @Override
    public void removeWithFlavor(Long[] ids) {
        for (Long id:ids){
            Dish dish = this.getById(id);
            Integer status = dish.getStatus();
            String dishName = dish.getName();
            if (status ==1 ){
                throw new CustomException(dishName + "启售中，不能删除");
            }
            this.removeById(id);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        }
    }

}




